package com.budwk.app.sqfwV2.service.baiyunqu;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.enums.DownLoadFileNameEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwCompleteflowService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.AbstractCustomerPdfCreateField;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowBusinessParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfPdfGenerator;
import com.budwk.app.sqfwDoc.codeGeneratorV2.pdfService.CompleteFlowCommonPdfService;
import com.budwk.app.sqfwDoc.codeGeneratorV2.result.GeneratorPdfWordResult;
import com.budwk.app.sqfwDoc.codeGeneratorV2.test.RefuseClassifyPdfGenerator;
import com.budwk.app.sqfwV2.dto.dto.AuthenticRightDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskPdfDTO;
import com.budwk.app.sqfwV2.enums.AuthenticRightEnum;
import com.budwk.app.sqfwV2.enums.SafetyCheckV2Enums;
import com.budwk.app.sqfwV2.generator.baiyunqv.AuthenticRightPdfGenerator;
import com.budwk.app.sqfwV2.generator.centuryCitySecurity.pdf.SafetyCheckPdfGenerater;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.google.common.collect.Lists;
import com.mysql.cj.util.StringUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.*;

@IocBean
public class AuthenticRightPdfService extends CompleteFlowCommonPdfService {

    @Inject
    SqfwCompleteflowService sqfwCompleteflowService;

    @Override
    public void createPdfAndMsg(CompleteFlowPdfParamForController paramForController) {
        super.createPdfAndMsg(paramForController);
    }


    @Override
    public GeneratorPdfWordResult createPdfSync(CompleteFlowPdfParamForController paramForController) {
        return super.createPdfSync(paramForController);
    }

    /**
     * 获取用户自定义字段 代码生成器已经自动填充
     * 无需特殊修改
     *
     * @param paramData json字符串
     * @return 结果
     */
    @Override
    protected AbstractCustomerPdfCreateField getCustomerPdfField(String paramData) {
        return JSONObject.parseObject(paramData, AuthenticRightDTO.class);
    }

    /**
     * 允许用户自定义填充在创建pdf时所提供的数据
     *
     * @param pdfGenerateParam 创建pdf所提供的数据
     */
    @Override
    protected void customerPopularPdfGenerateParam(PdfGenerateParam pdfGenerateParam) {


    }

    /**
     * 在这里设置生成pdf的所有generator
     *
     * @param pdfGenerateParam 创建pdf的参数
     * @return 生成pdf的generator合集
     */
    @Override
    protected List<BasePdfGenerator> getPdfGenerator(PdfGenerateParam pdfGenerateParam) {
        List<BasePdfGenerator> generatorList = Lists.newArrayList();
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        AuthenticRightDTO authenticRightDTO = JSON.parseObject(formSummary.getParamData(), AuthenticRightDTO.class);
        if (authenticRightDTO.getCaseType().equals(AuthenticRightEnum.A.getName())) {
            generatorList.add(new AuthenticRightPdfGenerator(AuthenticRightEnum.getPdfTemplatName(authenticRightDTO.getCaseType())));
        } else {
            String templatName = AuthenticRightEnum.getPdfTemplatName(authenticRightDTO.getCaseType());
            List<String> templatNameList = new ArrayList<>(Arrays.asList(templatName.split(",")));
            if (CollectionUtil.isNotEmpty(authenticRightDTO.getVisitPictures())) {
                templatNameList.add("/baiyunqv/pictures.docx");
            }
            templatNameList.forEach(s -> {
                generatorList.add(new AuthenticRightPdfGenerator(s));
            });
        }
        return generatorList;
    }

    /**
     * 用户自定义下载pdf文件的名称
     *
     * @param pdfGenerateParam 创建pdf所提供的参数
     * @return 结果
     */
    @Override
    protected String getDownloadFileName(PdfGenerateParam pdfGenerateParam) {
        CompleteFlowBusinessParam businessParam = (CompleteFlowBusinessParam) pdfGenerateParam.getBusinessParam();
        Sqfw_formSummary_new formSummary = businessParam.getFormSummary();
        AuthenticRightDTO authenticRightDTO = JSON.parseObject(formSummary.getParamData(), AuthenticRightDTO.class);
        return StrUtil.format(AuthenticRightEnum.getFileName(authenticRightDTO.getCaseType()), authenticRightDTO.getCaseNumber(), authenticRightDTO.getNoticeType());
    }

    /**
     * 第五张表中的需要的类型，详情见 {@link com.budwk.app.sqfw.constants.SqfwFormSummaryConstants}
     *
     * @return 返回数据
     */
    @Override
    public String getSummaryType() {
        return "12345确权";
    }


    protected void afterInsertForSummary(Sqfw_formSummary_new formSummary) {
        AuthenticRightDTO authenticRightDTO = JSON.parseObject(formSummary.getParamData(), AuthenticRightDTO.class);
        if (null != authenticRightDTO.getDownFlag() && authenticRightDTO.getDownFlag().equals("否")) {
            sqfwCompleteflowService.delete(formSummary.getSqfwFlowId());
        }
    }

}